package com.ggh.jinjilive.ui.social.near;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.core.LatLonPoint;
import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.FragmentNearUserBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.social.near.NearUser;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.mine.personal.PersonalDataActivity;
import com.ggh.jinjilive.ui.social.near.adapter.NearUserAdapter;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.ggh.jinjilive.utils.LocationUtils;
import com.google.gson.Gson;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class NearUserFragment extends BaseFragment<FragmentNearUserBinding> implements OnRefreshListener, OnLoadMoreListener {
    private NearViewModel nearViewModel;
    //附近适配器
    private NearUserAdapter nearUserAdapter;

    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        nearViewModel = new ViewModelProvider(this,viewModelFactory).get(NearViewModel.class);
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_near_user;
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        View contentView = binding.getRoot().findViewById(R.id.refreshLayout);
        loadService = LoadSir.getDefault().register(contentView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                onRefresh(binding.refreshLayout);
            }
        });
        loadService.showCallback(LoadingCallback.class);
    }

    /**
     * 更新的经纬度
     * @param latLonPoint
     */
    public void setLatLonPoint(LatLonPoint latLonPoint){
        if(null != nearViewModel){
            nearViewModel.latLonPointObservableField.set(latLonPoint);
            onRefresh(binding.refreshLayout);
        }
    }

    /**
     * 获取经纬度
     * @return
     */
    public LatLonPoint getLatLonPoint(){
        if(null != nearViewModel){
            return nearViewModel.latLonPointObservableField.get();
        }
        return null;
    }


    @Override
    public void initView() {
        super.initView();
        binding.refreshLayout.setRefreshHeader(new MaterialHeader(mContext));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));

        nearUserAdapter = new NearUserAdapter();
        nearUserAdapter.setAddItemOnClickListener(new AddItemOnClickListener() {
            @Override
            public void onSkipClicked(int position, Object mObject) {
                NearUser nearUser = (NearUser) mObject;
                if(null != nearUser){
                    Intent intent =new Intent(mContext, PersonalDataActivity.class);
                    intent.putExtra("nearUser",nearUser);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);//启动Activity
                }
            }
        });
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        binding.recyclerView.setAdapter(nearUserAdapter);
    }

    @Override
    public void initData() {
        super.initData();
        //定位信息
        LocationUtils.positioning(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                LocationUtils.stopLocation();
                LatLonPoint latLonPoint = new LatLonPoint(aMapLocation.getLatitude(),aMapLocation.getLongitude());
                nearViewModel.latLonPointObservableField.set(latLonPoint);
                onRefresh(binding.refreshLayout);
            }
        });
        LocationUtils.startLocation();
        nearViewModel.listMutableLiveData.observe(this, new Observer<List<NearUser>>() {
            @Override
            public void onChanged(List<NearUser> nearUsers) {
                binding.refreshLayout.finishRefresh();//结束刷新
                if(nearViewModel.isRefresh){
                    if(null != nearUsers && nearUsers.size() > 0){
                        nearUserAdapter.Add(nearUsers);
                        loadService.showSuccess();
                    }else {
                        loadService.showCallback(EmptyCallback.class);
                    }
                }else {
                    if(null != nearUsers && nearUsers.size() > 0){
                        nearUserAdapter.setAdd(nearUsers);
                        binding.refreshLayout.finishLoadMore();//结束刷新
                    }else {
                        binding.refreshLayout.finishLoadMoreWithNoMoreData();//结束刷新
                    }
                }
            }
        });
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //刷新
        binding.refreshLayout.setOnRefreshListener(this);
        //加载
        binding.refreshLayout.setOnLoadMoreListener(this);
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        nearViewModel.isRefresh = true;
        if(null != nearViewModel.latLonPointObservableField.get()){
            nearViewModel.nearbyUser();
        }
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        nearViewModel.isRefresh = false;
        if(null != nearViewModel.latLonPointObservableField.get()) {
            nearViewModel.nearbyUser();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }
}
